Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
How do I use a "range" to find a number in an array, then remove that index, and the one immediately the left of it?

I have an array like this:

const array = ["something", "6", "somethingelse", "130", "carrot", "89", "monkey", "57", "plane", "71"];

I need to be able to identify any index with "number value" less than 65, and remove that index and the one immediately to the left of it.

So in this case, numbers in the array which are less than 65 are "6" and "57".

I need to remove "6" and "something", as well as "57" and "monkey".

and the resulting array would be:

const array = ["somethingelse", "130", "carrot", "89", "plane", "71"];

I have the following code so far:

const array = ["something", "6", "somethingelse", "130", "carrot", "89", "monkey", "57", "plane", "71"];
const range = 65 //I need to specify a range here, and not an exact number
const remove_array_index = array.findIndex(a =>a.includes(range));
if(array > -1){  //having -1 in .splice returns unintended results, so this tests if an index was found that matches the range
  array.splice(remove_array_index-1, 2);
}

For the code above I need to specify the number value exactly, which removes that index and the one to the left of it..... The problem is that I need to specify a range, and not an exact value.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

this way ?

const array = 
  [ 'something',     '6'
  , 'somethingelse', '130'
  , 'carrot',        '89'
  , 'monkey',        '57'
  , 'plane',         '71'
  ] 
const range = 65

for (let index = array.length -1; index > 0; index -= 2)  
  {
  if (Number(array[index]) < range) array.splice(index-1, 2)
  }
  
console.log( JSON.stringify(array) )

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can easily achive the result using simple for loop

const array = [
  "something",
  "6",
  "somethingelse",
  "130",
  "carrot",
  "89",
  "monkey",
  "57",
  "plane",
  "71",
];

const result = [], range = [0, 65];
for (let i = 0; i < array.length; i += 2) {
  const str = array[i];
  const num = array[i + 1];

  if (range[0] < num && num > range[1]) result.push(str, num);
}
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could iterate all indices and check the value and remove, if necessary.

const
    remove = (array, [lower, upper]) => {
        let i = 0;
        
        while (i < array.length) {
            if (+array[i + 1] >= lower && +array[i + 1] <= upper) {
                array.splice(i, 2);
                continue;
            }        
            i += 2;
        }
        return array;
    },
    array = ["something", "6", "somethingelse", "130", "carrot", "89", "monkey", "57", "plane", "71"];

remove(array, [0, 65]);

console.log(array);

If you do not need the same object reference from array, you could filter the array.

const
    remove = (array, [lower, upper]) => array.filter((del => (_, i, { [ i + 1] : v }) => {
        if (del) return del = false;
        if (i % 2 === 0 && +v >= lower && +v <= upper) {
            del = true;
            return false;
        }
        return true;
    })()),

    array = ["something", "6", "somethingelse", "130", "carrot", "89", "monkey", "57", "plane", "71"];

console.log(remove(array, [0, 65]));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda